<template>
  <div class="todo-footer">
    <label>
      <input type="checkbox" v-model="checked" @change="changeCheck" />
    </label>
    <span>
      <span>已完成{{ finishedNum }}</span> / 全部{{ allNum }}
    </span>
    <button class="btn btn-danger" @click="clearAll">清除已完成任务</button>
  </div>
</template>


<script>
export default {
  name: "statistic",
  data() {
    return {
      finishedNum: 0,
      allNum: 0,
      checked: false,
    };
  },
  created() {
    //接受是否全选
    this.$bus.$on("allChecked", (allChecked) => {
      this.checked = allChecked;
    });
    //接受数量
    this.$bus.$on("finished_allNum", (finishedNum, allNum) => {
      this.finishedNum = finishedNum;
      this.allNum = allNum;
    });
  },
  methods: {
    changeCheck() {
      this.$bus.$emit("allCheckedClick", this.checked);
      this.finishedNum = this.checked ? this.allNum : 0;
    },
    clearAll() {
      //优化节流
      this.$bus.$emit("clearAll");
    },
  },
  beforeDestroy() {
    this.$bus.$off("allChecked");
    this.$bus.$off("finished_allNum");
  },
};
</script>

<style scoped>
/*footer*/
.todo-footer {
  height: 50px;
  line-height: 50px;
  padding-left: 6px;
  font-size: 14px;
  color: #606266;
  margin-top: 5px;
}
.todo-footer label {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}
.todo-footer label input {
  position: relative;
  top: -1px;
  vertical-align: middle;
  margin-right: 5px;
}
.todo-footer button {
  cursor: pointer;
  float: right;
  margin-top: 5px;
  border-radius: 3px;
  padding: 7px 15px;
  background-color: #f56c6c;
  border: 2px solid #f56c6c;
  color: #fff;
}
</style>